package com.example.my.meila.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.my.meila.R;
import com.example.my.meila.common.image.ImageLoader;
import com.example.my.meila.constast.Constast;
import com.example.my.meila.entity.Global;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/10/31.
 */
public class GlobalAdapter  extends RecyclerView.Adapter<GlobalAdapter.CardViewHolder> {
    private List<Global.DataBean.ModulesBean.ProductsBean> data;
    private Context context;
    private OnItemClickListener onItemClickListener;
    public GlobalAdapter(List<Global.DataBean.ModulesBean.ProductsBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public GlobalAdapter.CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater from = LayoutInflater.from(parent.getContext());
        View view = from.inflate(R.layout.item_rv_global, parent, false);
        CardViewHolder holder = new CardViewHolder(view);
        return holder;


    }


    @Override
    public void onBindViewHolder(GlobalAdapter.CardViewHolder holder, int position) {
        holder.tv1.setText(data.get(position).getName());
        holder.tv2.setText(data.get(position).getPurchase_source());
        holder.tv3.setText(String.valueOf(data.get(position).getPrice()));
        ImageLoader.getInstance().display(context, Constast.PUBLIC_URL+data.get(position).getImgs().get(0).getImg(), holder.iv);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class CardViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv1)
        TextView tv1;
        @BindView(R.id.tv2)
        TextView tv2;
        @BindView(R.id.tv3)
        TextView tv3;
        @BindView(R.id.iv)
        ImageView iv;
        public CardViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
           itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   onItemClickListener.OnItemClickClickListener(getLayoutPosition()-2);
               }
           });
        }
    }
    public interface OnItemClickListener{
        void OnItemClickClickListener(int postion);
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}